﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 文件夹清理工具
{
    public partial class 文件夹清理工具 : Form
    {
        DataTable _table;
        public 文件夹清理工具()
        {
            InitializeComponent();
        }

        private void 清理_Click(object sender, EventArgs e)
        {
            DataTable got = (DataTable)数据表.DataSource;
            YZZ.InformationController.SaveTable(System.Threading.Thread.GetDomain().BaseDirectory + "Data.json", got);
            foreach (DataRow row in got.Rows)
            {
                YZZ.FileHelperTemplate.RemoveDirectory(row["项目地址"].ToString() + "\\obj");
                YZZ.FileHelperTemplate.RemoveDirectory(row["项目地址"].ToString() + "\\bin");
            }
            MessageBox.Show("清理完成！");
        }

        private void 文件夹清理工具_Load(object sender, EventArgs e)
        {
            _table = new DataTable();
            _table.Columns.Add("项目名称", typeof(string));
            _table.Columns.Add("项目地址", typeof(string));
            DataTable loaded = YZZ.InformationController.LoadTable(System.Threading.Thread.GetDomain().BaseDirectory + "Data.json");
            if (loaded != null && loaded.Rows.Count > 0)
                _table = loaded.Copy();
            数据表.DataSource = _table;
            数据表.Columns[0].Width = 250;
            数据表.Columns[1].Width = 525;
            Viewer.DrawForm(this);
            Viewer.DrawDataGrid(数据表);
            数据表.Columns[1].ReadOnly = true;
        }

        protected override void OnResize(EventArgs e)
        {
            Viewer.DrawForm(this);
            Viewer.DrawDataGrid(数据表);
        }

        private void 双击数据表(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0)
                return;

            项目文件夹.ShowNewFolderButton = true;
            项目文件夹.RootFolder = Environment.SpecialFolder.Desktop;
            DataTable read = (DataTable)数据表.DataSource;
            if (read.Rows.Count>0 && read.Rows.Count > e.RowIndex && !string.IsNullOrEmpty(read.Rows[e.RowIndex][1].ToString()))
                项目文件夹.SelectedPath = read.Rows[e.RowIndex][1].ToString();
            DialogResult got = 项目文件夹.ShowDialog();
            switch (got)
            {
                case DialogResult.OK:
                    数据表.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = 项目文件夹.SelectedPath;
                    break;
            }
        }
    }
}
